import { Toast} from 'antd-mobile'
import { jwtDecode } from "jwt-decode";
import {useInterval} from 'ahooks'
import {getApplyOrgs, login} from "@/services";
import dayjs from "dayjs";

export default {
    namespace: 'global',
    state: {
        user: {},
        jwt: "",
        jwtIntervalTime: undefined
    },
    reducers: {
        add(state: any) {
            // state.num += 1;
            return {...state, num: state.num + 1};
        },
        updateUser(state, {payload: {user, jwt}}) {
            return {...state, user, jwt}
        },
        updateJwtIntervalTime(state, {payload: {value}}) {
            state.jwtIntervalTime = value;
        }
    },
    effects: {
        * addAsync(_action: any, {call, put}: any) {
            const response = yield call(getApplyOrgs)
            console.log("************111")
            console.log(response)
            const {code, message, data} = response;
            yield put({type: 'add'});
        },
        * login({payload}, {call, put}) {
            const response = yield call(login);
            console.log("************2222")
            console.log(response)
            const {code, message, data} = response;
            if (code === "0") {
                // 请求成功
                const token = data.token;
                const user = jwtDecode(token);
                sessionStorage.setItem("jwt", token);
                sessionStorage.setItem("username", data.username);
                sessionStorage.setItem("usercode", data.usercode);
                sessionStorage.setItem("expireTime", data.expireTime);
                // sessionStorage.setItem("expireTime", dayjs().valueOf() + 10 * 1000);
                sessionStorage.setItem("depts", JSON.stringify(data.depts));
                sessionStorage.setItem("user", JSON.stringify(user));
                console.log("!!!");
                console.log(user["depts"])
                console.log( JSON.stringify(data.depts))
                yield put({
                    type: 'updateUser',
                    payload: {
                        user: {},
                        jwt: data.token
                    }
                })
                yield put({
                    type: 'updateJwtIntervalTime',
                    payload: {
                        value: 1000
                    }
                })
            } else {
                // 失败 弹窗
                Toast.show({
                    icon: 'fail',
                    content: '无法获取到登录信息',
                })
            }
            // yield put({type: 'add'});
        }
    },
    subscriptions: {
        setup({dispatch}) {
            document.addEventListener('readystatechange', e => {
                console.log(document.readyState)
                if (document.readyState == "complete") {
                    dispatch({
                        type: 'login'
                    });
                }
            });
        },
        checkJwt({dispatch}) {
            const jwtInterval = useInterval(()=> {
                const expireTime = sessionStorage.getItem("expireTime");
                // console.log(dayjs().valueOf())
                // console.log(expireTime)
                // console.log(dayjs().valueOf() - expireTime)

                if (expireTime < dayjs().valueOf()) {
                    dispatch({
                        type: 'updateJwtIntervalTime',
                        payload: {
                            value: undefined
                        }
                    });
                    dispatch({
                        type: 'login'
                    });
                }
            }, 1000)
        }
    },
};
